﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Config
{
    public class TestPointList
    {
        XmlNode _xmlNode = null;
        XmlDocument _xmlDoc = null;
        private List<TestPoint> _list = null;
        public TestPointList(XmlNode Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _xmlNode = Node;
            _xmlDoc = _xmlNode.OwnerDocument;

        }
        public XmlNode XmlNode
        {
            get
            {
                return _xmlNode;
            }
        }
        public void AddTestPoint(int id)
        {
            try
            {
                XmlNode node = _xmlDoc.CreateElement("item");
                TestPoint ch = new TestPoint(node);
                ch.Initial();
                ch.No = id;
                _xmlNode.AppendChild(ch.XmlNode);
            }
            catch (Exception ex)
            {

            }
        }

        public List<TestPoint> TestPoints
        {
            get
            {
                // List<BaseNode> lst = new List<BaseNode>();
                TestPoint c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<TestPoint>();

                foreach (XmlNode x in _xmlNode.SelectNodes("item"))
                {
                    c = new TestPoint(x);
                    //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
                //lst.Sort(delegate(BaseNode x, BaseNode y) { return x.id.CompareTo(y.id); });
                //foreach (BaseNode b in lst)
                //{
                //    _list.Add(b as Cls_TestPoint);
                //}
                _list.Sort();
                return _list;
            }
        }
    }
    public class TestPoint:BaseNode
    {
        public TestPoint(XmlNode Node):base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
        }
        public void Initial()
        {
            _xmlNode.Attributes.Append(_xmlDoc.CreateAttribute("NO")).Value = "";
            XmlNode node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("TestPointname"));
            node.InnerText = "";
            node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("enable"));
            node.InnerText = "";
        }
        public XmlNode XmlNode
        {
            get
            {
                return _xmlNode;
            }
        }
        //public string Name
        //{
        //    get
        //    {
        //        return _xmlNode.Name;
        //    }
        //}
        //public int No
        //{
        //    get
        //    {
        //        if (_xmlNode.Attributes["NO"] == null)
        //        {
        //            return -1;
        //        }
        //        return int.Parse(_xmlNode.Attributes["NO"].Value);
        //    }
        //    set
        //    {
        //        if (_xmlNode.Attributes["NO"] == null)
        //        {
        //            XmlDocument doc = _xmlNode.OwnerDocument;
        //            _xmlNode.Attributes.Append(doc.CreateAttribute("NO")).Value = value.ToString();
        //        }
        //        else
        //            _xmlNode.Attributes["NO"].Value = value.ToString();
        //    }
        //}
        public string TestPointName
        {
            get
            {
                if (_xmlNode == null)
                {
                    return "";
                }
                return _xmlNode["channelname"].InnerText;
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelname");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelname"));
                Node.InnerText = value.ToString();
            }
        }
        public int Enable
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["enable"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("enable");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("enable"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelname>测点1</channelname>
        //<enable>1</enable>
        //<channelxindex>0</channelxindex>
        public int channelxindex
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["channelxindex"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelxindex");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelxindex"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelyindex>1</channelyindex>
        public int channelyindex
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["channelyindex"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelyindex");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelyindex"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelvola>0</channelvola>
        public float channelvola
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["channelvola"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelvola");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelvola"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelvolb>0</channelvolb>
        public float channelvolb
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["channelvolb"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelvolb");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelvolb"));
                Node.InnerText = value.ToString();
            }
        }
        //<bearhousx>60</bearhousx>
        public float bearhousx
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["bearhousx"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("bearhousx");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("bearhousx"));
                Node.InnerText = value.ToString();
            }
        }
        //<bearhousy>60</bearhousy>
        public float bearhousy
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["bearhousy"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("bearhousy");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("bearhousy"));
                Node.InnerText = value.ToString();
            }
        }
        //<iniaxlex>0.0</iniaxlex>
        public float iniaxlex
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["iniaxlex"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("iniaxlex");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("iniaxlex"));
                Node.InnerText = value.ToString();
            }
        }
        //<iniaxley>0.0</iniaxley>
        public float iniaxley
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["iniaxley"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("iniaxley");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("iniaxley"));
                Node.InnerText = value.ToString();
            }
        }
        //<bearhori>1</bearhori>
        public float bearhori
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["bearhori"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("bearhori");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("bearhori"));
                Node.InnerText = value.ToString();
            }
        }
        //<bearveri>1</bearveri>
        public float bearveri
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1.0f;
                }
                return float.Parse(_xmlNode["bearveri"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("bearveri");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("bearveri"));
                Node.InnerText = value.ToString();
            }
        }
        //<setiniposflag>3</setiniposflag>
        public int setiniposflag
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["setiniposflag"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("setiniposflag");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("setiniposflag"));
                Node.InnerText = value.ToString();
            }
        }
        //<saveiniposflag>0</saveiniposflag>
        public int saveiniposflag
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["saveiniposflag"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("saveiniposflag");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("saveiniposflag"));
                Node.InnerText = value.ToString();
            }
        }
    }
}
